package com.zhss.dfs.upgrade;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * @description: 全局请请求处理
 * @Author liu Weidong
 * @Email:
 * @Date: 2024/6/2 14:09
 * @Version 1.0
 */
public class NetworkResponseQueue {


    private Map<String, ConcurrentLinkedQueue<NetworkResponse>> responseQueues =
            new HashMap<String, ConcurrentLinkedQueue<NetworkResponse>>();

    private NetworkResponseQueue(){

    }

    private static volatile NetworkResponseQueue networkResponseQueue;

    public static NetworkResponseQueue get(){
        if (networkResponseQueue == null) {
            synchronized (NetworkResponseQueue.class) {
                if (networkResponseQueue == null) {
                    networkResponseQueue = new NetworkResponseQueue();
                }
            }
        }
        return networkResponseQueue;
    }

    public void initResponseQueue(String processorId) {
        ConcurrentLinkedQueue<NetworkResponse> responseQueue =
                new ConcurrentLinkedQueue<NetworkResponse>();
        responseQueues.put(processorId, responseQueue);
    }

    public void offer(String processorId, NetworkResponse response) {
        responseQueues.get(processorId).offer(response);
    }

    public NetworkResponse poll(String processorId) {
        return responseQueues.get(processorId).poll();
    }
}
